package com.order.dao.impl;

import java.util.List;

import javax.annotation.Resource;

import org.hibernate.criterion.DetachedCriteria;
import org.springframework.orm.hibernate5.HibernateTemplate;
import org.springframework.stereotype.Repository;

import com.order.dao.IUserDao;
import com.order.domain.User;

@Repository("userDao")
public class UserDaoImpl implements IUserDao {
	
	@Resource(name="hibernateTemplate")
	private HibernateTemplate hibernateTemplate;
	
	@SuppressWarnings("unchecked")
	@Override
	public List<User> findAll(DetachedCriteria uCriteria) {
		return (List<User>) hibernateTemplate.findByCriteria(uCriteria);
	}

	@Override
	public void save(User user) {
		hibernateTemplate.save(user);

	}

	@Override
	public void delete(Integer userId) {
		hibernateTemplate.delete(findById(userId));

	}

	@Override
	public User findById(Integer userId) {
		User u = hibernateTemplate.load(User.class, userId);
		return u;
	}

	@Override
	public void update(User user) {
		hibernateTemplate.update(user);

	}

	@SuppressWarnings("unchecked")
	@Override
	public User findByUserNameAndPwd(String userName, String password) {
		List<User> users = (List<User>) hibernateTemplate.find("from User where userName = ? and password = ? ", userName , password);
		if(users != null && users.size()>0 ) {
			return users.get(0);
		}
		return null;
	}

	/**
	 * 更新学生数量
	 */
	@Override
	public void update(Integer userId , Integer studentNum) {
		User u = hibernateTemplate.get(User.class, userId);
		u.setStudentNum(studentNum);
		hibernateTemplate.update(u);
	}
	
}
